<template>
  <div class="card" style="width: 50%">
    <el-form ref="fromRef" :model="data.user" label-width="100px" style="padding: 20px 30px 10px 0;">
      <el-form-item prop="avatar">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/files/upload"
            :show-file-list="false"
            :headers="{ token: data.user.token }"
            :on-success="handleAvatarSuccess"
            list-type="picture"
        >
          <img v-if="data.user?.avatar" :src="data.user?.avatar" class="avatar"  alt=""/>
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item prop="username" label="账号" >
        <el-input v-model="data.user.username" placeholder="请输入账号" autocomplete="off" />
      </el-form-item>
      <el-form-item prop="name" label="用户名">
        <el-input v-model="data.user.name" placeholder="请输入用户名" autocomplete="off" />
      </el-form-item>
      <el-form-item prop="phone" label="手机号">
        <el-input v-model="data.user.phone" placeholder="请输入手机号" autocomplete="off" />
      </el-form-item>
      <el-form-item v-if="data.user.role === 'USER'" prop="address" label="地址">
        <el-input v-model="data.user.address" placeholder="请输入地址" autocomplete="off" />
      </el-form-item>
      <el-form-item v-else prop="email" label="邮箱">
        <el-input v-model="data.user.email" placeholder="请输入邮箱" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button type="primary" style="padding: 10px 20px" @click="update">保存</el-button>
      <el-button style="padding: 10px 20px" @click="goBack">返回</el-button>
    </div>
  </div>
</template>

<script setup>
import {reactive} from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

const data = reactive({
  user: JSON.parse(localStorage.getItem("code_user") || '{}'),
})

const handleAvatarSuccess = (res) => {
  data.user.avatar = res.data
}

const emit = defineEmits(['updateUser'])

const update = () => {
  let url
  if (data.user.role === 'ADMIN'){
    url = '/admin/update'
  }
  if (data.user.role === 'USER'){
    url = '/user/update'
  }
  request.put(url, data.user).then(res => {
    if (res.code === '200'){
      ElMessage.success('更新成功')
      localStorage.setItem('code_user', JSON.stringify(data.user))
      emit('updateUser')
    }
    }
  )
}

const goBack = () => {
  window.history.go(-1)
}

</script>

<style scoped>

.avatar-uploader .avatar {
  height: 178px;
  display: block;
}

</style>

<style>

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

</style>